package com.xdwanj.criminalintent.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xdwanj.criminalintent.R;
import com.xdwanj.criminalintent.adapter.CrimeAdapter;
import com.xdwanj.criminalintent.pojo.Crime;
import com.xdwanj.criminalintent.viewmodel.CrimeListViewModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

//这个Fragment中只有一个RecyclerView
public class CrimeListFragment extends Fragment {
    private final static String TAG = "CrimeListFragment";

    public interface Callbacks {
        void onCrimeSelected(UUID crimeId);
    }

    private Callbacks callbacks;

    //数据视图模块,保存数据
    private CrimeListViewModel crimeListViewModel;

    //列表视图类,与ListView类似
    private RecyclerView crimeRecyclerView;

    //内容适配器
    private CrimeAdapter crimeAdapter = new CrimeAdapter(this, new ArrayList<Crime>(), callbacks);
    //这里传入了空数据,因为这个Adapter不可以为空,UI线程展示数据需要适配器

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //绑定ViewModel
        crimeListViewModel = new ViewModelProvider(this).get(CrimeListViewModel.class);

        //设置选项菜单
        setHasOptionsMenu(true);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        callbacks = (Callbacks) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //将Fragment绑定,并给予MainActivity托管
        View view = inflater.inflate(R.layout.fragment_list_crime, container, false);

        //获取fragment_list_crime中的根标签,作为返回值绑定
        crimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view);

        //设置这个RecyclerView的布局格式为LinerLayout
        crimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        crimeRecyclerView.setAdapter(crimeAdapter); //将空crimeAdapter绑定在RecyclerView上
        return view; //绑定
    }

    @Override
    //在onCreateView执行完后调用
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        crimeListViewModel.crimeListLiveData.observe(
                this.getViewLifecycleOwner(), //(fragment视图)登记并绑定生命周期
                crimes -> { //onChange方法,当数据更新,这个观察者对象就会调用这个方法
                    if (crimes != null && crimes.size() != 0) {
                        Log.i(TAG, "onViewCreated: Got crimes " + crimes.size());
                        updateUI(crimes); //更新UI
                    } else {

                    }
                }
        );

    }

    /**
     * 更新UI
     *
     * @param crimes 用于更新的数据
     */
    private void updateUI(List<Crime> crimes) {
        //将数据交个适配器,并交给内容适配器
        crimeAdapter = new CrimeAdapter(this, crimes, callbacks);

        //设置RecyclerView中的Adapter(内容适配器)
        crimeRecyclerView.setAdapter(crimeAdapter);
    }


    /**
     * 当这个Fragment分离后执行
     */
    @Override
    public void onDetach() {
        super.onDetach();
        callbacks = null; //将这个回调接口对象设空
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

        inflater.inflate(R.menu.fragment_crime_list, menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.new_crime) {
            Log.d(TAG, "onOptionsItemSelected: on click option item" + item.getItemId());
            Crime crime = new Crime(UUID.randomUUID(), "", new Date(), false, "");
            crimeListViewModel.addCrime(crime);
            callbacks.onCrimeSelected(crime.getId());
            return true;
        }

        return super.onOptionsItemSelected(item);

    }

    @Override
    public void onStart() { //一般Fragment中的事件在这里绑定,防止转屏之类的方式破坏事件
        super.onStart();
    }


    //这个方法有什么意义? 摒弃类自带的构造方法,进一步提高自定义能力
    public static CrimeListFragment newInstance() {
        return new CrimeListFragment();
    }
}
